package com.zmn.oms.common.discount;

import com.zmn.common.constant.FicoConsts;
import com.zmn.oms.common.discount.dto.CalculateDiscountListDTO;
import com.zmn.oms.common.discount.dto.CalculateDiscountResultDTO;
import com.zmn.oms.common.discount.impl.CouponDiscountCalculatorImpl;
import com.zmn.oms.common.discount.impl.MasterDiscountCalculatorImpl;
import com.zmn.oms.common.discount.impl.MemberDiscountCalculatorImpl;
import com.zmn.oms.common.discount.impl.OperatorDiscountCalculatorImpl;
import com.zmn.oms.common.discount.interfaces.DiscountCalculateRuleService;
import com.zmn.oms.common.dto.ResultDTO;

import java.util.ArrayList;
import java.util.List;

/**
 * 类描述：优惠工厂
 *
 * @author heciqi
 * @date 2018/12/07 11:19
 */
public class DiscountFactory {


    public static DiscountCalculateRuleService create(int discountCateg) {

        DiscountCalculateRuleService calculateRuleService = null;

        switch (discountCateg) {
            case FicoConsts.ORDER_IN_DISCOUNT_CATEG_ARTIFICIAL_MASTER: // 人工优惠（工程师）
                calculateRuleService = new MasterDiscountCalculatorImpl();
                break;
            case FicoConsts.ORDER_IN_DISCOUNT_CATEG_ARTIFICIAL_OPERATOR: // 人工优惠（后台）
                calculateRuleService = new OperatorDiscountCalculatorImpl();
                break;
            case FicoConsts.ORDER_IN_DISCOUNT_CATEG_MEMBER:
                calculateRuleService = new MemberDiscountCalculatorImpl(); // 会员优惠
                break;
            case FicoConsts.ORDER_IN_DISCOUNT_CATEG_COUPON:
                calculateRuleService = new CouponDiscountCalculatorImpl(); // 优惠券
                break;
            default:
        }

        return calculateRuleService;
    }


    public static void main(String[] args) {
        DiscountCalculateRuleService calculateRuleService = DiscountFactory.create(FicoConsts.ORDER_IN_DISCOUNT_CATEG_ARTIFICIAL_MASTER);

        List<CalculateDiscountListDTO> list = new ArrayList<>();
        CalculateDiscountListDTO dto1 = new CalculateDiscountListDTO();
        dto1.setAmount(10);
        dto1.setCateg(FicoConsts.ORDER_IN_DISCOUNT_CATEG_ARTIFICIAL_MASTER);
        list.add(dto1);
        CalculateDiscountListDTO dto2 = new CalculateDiscountListDTO();
        dto2.setAmount(30);
        dto1.setCateg(FicoConsts.ORDER_IN_DISCOUNT_CATEG_MEMBER);
        list.add(dto2);

        ResultDTO resultDTO = calculateRuleService.canDiscount(list);
        if (!resultDTO.isStatusBool()) {
            System.out.println(resultDTO.getMessage());
        }
        CalculateDiscountResultDTO calculate = calculateRuleService.calculate(list, 1001, (amount -> (int) Math.floor(amount * 0.01)));
        System.out.println(calculate);
    }
}
